<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\common\helper\util\Page;
use app\exam\model\admin\PaperTopicLink as PaperTopicLinkModel;

/**
 * Class PaperTopicLink
 *
 * @package app\exam\logic\admin
 */
class PaperTopicLink extends PaperTopicLinkModel
{
    /**
     * 清理关联的信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/17
     *
     * @param int $paperId
     * @return int
     */
    public function clearLinks(int $paperId): int
    {
        $results = $this->where('paper_id', $paperId)->select();
        foreach ($results as $result) {
            $this->where('paper_id', $paperId)->where('topic_id', $result['topic_id'])->find()->delete();
        }

        return $results->count();
    }

    /**
     * 获取试题关联的试卷分页数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/18
     *
     * @param int $topicId
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getQuestionTopicLinkPapers(int $topicId)
    {
        return $this->with(['paper'])->where('topic_id', $topicId)->paginate(Page::listRows());
    }

}